package com.cskaoyan.market.config;

import com.cskaoyan.market.filter.MyCorsFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;


/**
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2024/5/17 17:31
 * @Version 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {





    //向容器中去注册一个objectMAPPER,因为框架内部的objectMapper没有设置过时间，处理时间时可能会有问题
    @Bean
    public ObjectMapper objectMapper() {
        String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
        String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
        String DEFAULT_TIME_FORMAT = "HH:mm:ss";
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        // 增加LocalDate和LocalTime和LocalDateTime的解析格式的支持
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }


    //注册配置filter

/*    @Bean
    public FilterRegistrationBean encodingFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setEncoding("UTF-8");

        registrationBean.setFilter(characterEncodingFilter);
        registrationBean.setOrder(1);  //值越小，Filter越靠前。
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }


*//*    @Bean
    public FilterRegistrationBean corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        FilterRegistrationBean filterBean = new FilterRegistrationBean();

        //"Access-Control-Allow-Headers","x-requested-with,Authorization,Content-Type,X-CskaoyanMarket-Admin-Token,X-CskaoyanMarket-Token"
        corsConfiguration.addAllowedHeader("Access-Control-Allow-Headers");//允许跨域的请求头
        corsConfiguration.addAllowedHeader("x-requested-with,Authorization,Content-Type,X-CskaoyanMarket-Admin-Token,X-CskaoyanMarket-Token");//允许跨域的请求头
        corsConfiguration.addAllowedOrigin("http://localhost:9527/");//允许的域名，*表示所有，也可以是具体的域名
        corsConfiguration.addAllowedMethod("POST,GET,OPTIONS,PUT,DELETE");//允许跨域的请求
        corsConfiguration.setAllowCredentials(true);//允许跨域标志位
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);//对哪些路径进行跨域设置，**表示所有
        CorsFilter corsFilter = new CorsFilter(urlBasedCorsConfigurationSource);

        //优先级设为最高
        FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<>(corsFilter);
        registrationBean.setOrder(3);
        return registrationBean;
    }*//*

    @Bean
    public FilterRegistrationBean authFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(authFilter);
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("authFilter");
        registrationBean.setOrder(2);  //值越小，Filter越靠前。
        return registrationBean;

    }*/


    @Bean
    public FilterRegistrationBean corsFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyCorsFilter());
        registrationBean.addUrlPatterns("/admin/*");
        registrationBean.setName("myCorsFilter");
        registrationBean.setOrder(3);  //值越小，Filter越靠前。
        return registrationBean;
    }

}
